package org.corey.share.misc;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Common {
	public static boolean isEmpty(Object obj) {
		if (obj instanceof String) {
			String str = (String) obj;
			if (str == null || str.isEmpty()) {
				return true;
			}
		}
		return obj == null;
	}

	public static String parseDate(Date date) {
		DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		return format.format(date);
	}

	public static Long parseLong(Object obj) {
		if (obj instanceof String) {
			return Long.parseLong((String) obj);
		} else {
			return (Long) obj;
		}
	}

	public static String serializeDate(Date date) {
		DateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
		return format.format(date);
	}

	public static String getExtendNameWithDot(String fileName) {
		return fileName.substring(fileName.lastIndexOf("."));
	}

	public static String getExtendNameWithoutDot(String fileName) {
		return fileName.substring(fileName.lastIndexOf(".") + 1);
	}

	/**
	 * IE6 IE7不同，文件上传的时候会传入整个路径或者是单单文件名
	 * @param filePath
	 * @return
	 */
	public static String getFileNameFromFilePath(String filePath) {
		if (filePath.indexOf("\\") > 0) {
			return filePath.substring(filePath.lastIndexOf("\\")+1);
		}
		if (filePath.indexOf("/") > 0) {
			return filePath.substring(filePath.lastIndexOf("/")+1);
		}
		return filePath;
	}
	
	public static void main(String[] args) {
		System.out.println(getFileNameFromFilePath("F:/CoreyShare/war/a.txt"));
	}

}
